namespace PMS.BLL
{
    using PMS.DBUtility;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class JobPositionBLL
    {
        public int Add(JobPosition model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into UserJobPosition(");
            builder.Append("PositionName,IsLeader");
            builder.Append(") values (");
            builder.Append("@PositionName,");
            builder.Append("@IsLeader");
            builder.Append(") ");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PositionName", SqlDbType.NVarChar, 50), new SqlParameter("@IsLeader", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.PositionName;
            cmdParms[1].Value = model.IsLeader;
            object single = DBHelper.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int TitleID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from UserJobPosition ");
            builder.Append(" where PositionID=@PositionID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PositionID", SqlDbType.Int, 4) };
            cmdParms[0].Value = TitleID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool Exists(int TitleID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(PositionID) from UserJobPosition");
            builder.Append(" where ");
            builder.Append(" PositionID = @PositionID  ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PositionID", SqlDbType.Int, 4) };
            cmdParms[0].Value = TitleID;
            return DBHelper.Exists(builder.ToString(), cmdParms);
        }

        public List<JobPosition> GetList()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * ");
            builder.Append(" FROM UserJobPosition  ");
            DataSet set = DBHelper.Query(builder.ToString());
            List<JobPosition> list = new List<JobPosition>();
            if (set.Tables[0].Rows.Count > 0)
            {
                JobPosition item = null;
                foreach (DataRow row in set.Tables[0].Rows)
                {
                    item = new JobPosition();
                    if (row["PositionID"].ToString() != "")
                    {
                        item.PositionID = int.Parse(row["PositionID"].ToString());
                    }
                    item.PositionName = row["PositionName"].ToString();
                    item.IsLeader = int.Parse(row["IsLeader"].ToString());
                    list.Add(item);
                }
            }
            return list;
        }

        public JobPosition GetModel(int pid)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select PositionID,PositionName ,IsLeader ");
            builder.Append("  from UserJobPosition ");
            builder.Append(" where PositionID=@PositionID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PositionID", SqlDbType.Int, 4) };
            cmdParms[0].Value = pid;
            JobPosition position = new JobPosition();
            DataSet set = DBHelper.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["PositionID"].ToString() != "")
                {
                    position.PositionID = int.Parse(set.Tables[0].Rows[0]["PositionID"].ToString());
                }
                position.PositionName = set.Tables[0].Rows[0]["PositionName"].ToString();
                position.IsLeader = int.Parse(set.Tables[0].Rows[0]["IsLeader"].ToString());
                return position;
            }
            return null;
        }

        public bool Update(JobPosition model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update UserJobPosition set ");
            builder.Append(" IsLeader = @IsLeader , ");
            builder.Append(" PositionName = @PositionName  ");
            builder.Append(" where PositionID=@PositionID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@PositionID", SqlDbType.Int, 4), new SqlParameter("@PositionName", SqlDbType.NVarChar, 50), new SqlParameter("@IsLeader", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.PositionID;
            cmdParms[1].Value = model.PositionName;
            cmdParms[2].Value = model.IsLeader;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

